package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.ZtUserVipBo;
import org.dromara.system.domain.vo.ZtUserVipVo;
import org.dromara.system.domain.ZtUserVip;
import org.dromara.system.mapper.ZtUserVipMapper;
import org.dromara.system.service.IZtUserVipService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户VIPService业务层处理
 *
 * @author Lion Li
 * @date 2024-06-26
 */
@RequiredArgsConstructor
@Service
public class ZtUserVipServiceImpl implements IZtUserVipService {

    private final ZtUserVipMapper baseMapper;

    /**
     * 查询用户VIP
     */
    @Override
    public ZtUserVipVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户VIP列表
     */
    @Override
    public TableDataInfo<ZtUserVipVo> queryPageList(ZtUserVipBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZtUserVip> lqw = buildQueryWrapper(bo);
        Page<ZtUserVipVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户VIP列表
     */
    @Override
    public List<ZtUserVipVo> queryList(ZtUserVipBo bo) {
        LambdaQueryWrapper<ZtUserVip> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZtUserVip> buildQueryWrapper(ZtUserVipBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZtUserVip> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, ZtUserVip::getUserId, bo.getUserId());
        lqw.eq(bo.getEffectiveEndDate() != null, ZtUserVip::getEffectiveEndDate, bo.getEffectiveEndDate());
        return lqw;
    }

    private Wrapper<ZtUserVip> buildQueryWrapper2(ZtUserVipBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<ZtUserVip> lqw = Wrappers.query();
        lqw.eq(bo.getUserId() != null, "user_id", bo.getUserId());
        lqw.eq(bo.getEffectiveEndDate() != null, "effective_end_date", bo.getEffectiveEndDate());
        lqw.like(params.get("nickName") != null,"nick_name", params.get("nickName"));
        lqw.like(params.get("phonenumber") != null,"phonenumber", params.get("phonenumber"));
        return lqw;
    }

    /**
     * 新增用户VIP
     */
    @Override
    public Boolean insertByBo(ZtUserVipBo bo) {
        ZtUserVip add = MapstructUtils.convert(bo, ZtUserVip.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户VIP
     */
    @Override
    public Boolean updateByBo(ZtUserVipBo bo) {
        ZtUserVip update = MapstructUtils.convert(bo, ZtUserVip.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZtUserVip entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户VIP
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<ZtUserVipVo> queryPageList_join_user(ZtUserVipBo bo, PageQuery pageQuery) {
        Wrapper<ZtUserVip> lqw = buildQueryWrapper2(bo);
        Page<ZtUserVipVo> result = baseMapper.queryPageList_join_user(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public ZtUserVipVo queryVipUser(Long userId) {
        return baseMapper.queryVipUser(userId);
    }
}
